package com.hairline.controller;

import com.hairline.bean.Storage;
import com.hairline.bean.vo.BaseRespVo;
import com.hairline.service.AdminStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@RestController
public class AdminStorageController {
    @Value("${upload.file.path}")
    String path;

    @Autowired
    AdminStorageService adminStorageService;

    @RequestMapping("/admin/storage/list")
    public BaseRespVo storageList(Integer page, Integer limit, String sort, String order,String key ,String name){

        Map<String,Object> map = adminStorageService.getStorageList(page, limit, sort, order, key, name);
        return BaseRespVo.ok(map);
    }

    @PostMapping("/admin/storage/create")
    public BaseRespVo storageCreate(MultipartFile file) throws IOException {
        String filename = UUID.randomUUID().toString() + ".jpg";
        File file1 = new File(path, filename);
        file.transferTo(file1);
        Storage storage = adminStorageService.create(file,filename);
        return BaseRespVo.ok(storage);
    }

    @PostMapping("/admin/storage/update")
    public BaseRespVo storageUpdate(@RequestBody Storage storage){
        Storage storage1 = adminStorageService.updateStorage(storage);
        return BaseRespVo.ok(storage1);
    }

    @PostMapping("/admin/storage/delete")
    public BaseRespVo storageDelete(@RequestBody Storage storage){
        adminStorageService.deleteStorage(storage);
        return BaseRespVo.ok();
    }
}
